<?php


namespace app\admin\controller;


use app\common\controller\Adminbase;
use PhpOffice\PhpSpreadsheet\IOFactory;
use think\Db;

class Cmap extends Adminbase
{
    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            //获取列表
            $business_where[] = ['is_del', '=', 0];
            $filter = input('filter');
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['id'])) {
                    $business_where[] = ['id', '=', $filter['id']];
                }

                if (isset($filter['cate_id'])) {
                    $business_where[] = ['cate_id', '=', $filter['cate_id']];
                }

                if (isset($filter['xm_type'])) {
                    $business_where[] = ['xm_type', '=', $filter['xm_type']];
                }
                if (isset($filter['name'])) {
                    $business_where[] = ['name', 'like', '%' . $filter['name'] . '%'];
                }
                if (isset($filter['c_address'])) {
                    $business_where[] = ['address', 'like', '%' . $filter['c_address'] . '%'];
                }
                if (isset($filter['province'])) {
                    $province = Db::name('area')->find($filter['province']);
                    $business_where[] = ['province', '=', $province['name']];
                }
                if (isset($filter['city'])) {
                    $city = Db::name('area')->find($filter['city']);
                    $business_where[] = ['city', '=', $city['name']];
                }

                if (isset($filter['create_time'])) {
                    $create_time = explode(' - ', $filter['create_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $business_where[] = ['create_time', 'between', [$create_time[0], $create_time[1]]];
                }
            }
            $_list = Db::name('cmap')
                ->page($page, $limit)
                ->where($business_where)
                ->order('create_time desc')
                ->select();

            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                $_list[$k]['c_address'] = $v['province'] . $v['city'] . $v['area'] . $v['address'];
                //资产类型
                $cate_info = Db::name('cmap_cate')->find($v['cate_id']);
                $_list[$k]['cate_name'] = $cate_info['name'];
            }
            //获取数量
            $total = Db::name('cmap')
                ->where($business_where)
                ->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $area_list = Db::name('area')->where([
            ['id', '<>', 0],
            ['pid', '=', 0]
        ])->select();
        foreach ($area_list as $k => $v) {
            $proviec[$v['id']] = $v['name'];
        }
        $type_list2 = Db::name('cmap_cate')
            ->where(['is_del' => 0])
            ->order('sort desc')
            ->select();
        foreach ($type_list2 as $k => $v) {
            $cate_list[$v['id']] = $v['name'];
        }
        $this->assign('proviec', $proviec);
        $this->assign('cate_list', $cate_list);
        return $this->fetch();
    }

    public function add()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $address = getLWByAddress($data['row']['province'] . $data['row']['city'] . $data['row']['area'] . $data['row']['address']);
            $address = json_decode($address, true);
            if ($address['code'] == 0) {
                $data['row']['longitude'] = $address['data'][0];
                $data['row']['latitude'] = $address['data'][1];
                $data['row']['is_c'] = 2;
            } else {
                $this->error('输入正确位置');
            }
            $data['row']['create_time'] = time();

            $data['row']['start_time'] = strtotime($data['row']['start_time']);

            $data['row']['zong_value'] = $data['row']['reference_value'] + $data['row']['debt_interest'];
            $cmap = Db::name('cmap')->insertGetId($data['row']);
            $this->success('操作成功');
        } else {
            //资产类型
            $type_list = Db::name('cmap_cate')
                ->where(['is_del' => 0])
                ->order('sort desc')
                ->select();
            $proviec_list = Db::name('area')->where([
                ['id', '<>', 0],
                ['pid', '=', 0]
            ])->select();
            $this->assign([
                'proviec' => $proviec_list,
                'type_list' => $type_list,
            ]);
            return $this->fetch();
        }
    }

    //删除
    public function del()
    {
        $ids = $this->request->param('id/a', null);
        try {
            //软删除
            foreach ($ids as $id) {
                Db::name('cmap')->where('id', $id)->delete();
//                Db::name('cmap')->where('id', $id)->update([
//                    'is_del'=>1
//                ]);
            }
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('删除成功！');
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $address = getLWByAddress($data['row']['province'] . $data['row']['city'] . $data['row']['area'] . $data['row']['address']);
            $address = json_decode($address, true);
            if ($address['code'] == 0) {
                $data['row']['longitude'] = $address['data'][0];
                $data['row']['latitude'] = $address['data'][1];
                $data['row']['is_c'] = 2;
            } else {
                $this->error('输入正确位置');
            }
            $data['row']['start_time'] = strtotime($data['row']['start_time']);
            $data['row']['zong_value'] = $data['row']['reference_value'] + $data['row']['debt_interest'];
            $cmap = Db::name('cmap')->update($data['row']);
            $this->success('操作成功');
        } else {
            $id = $this->request->param('id/d', 0);
            $data = Db::name('cmap')->find($id);
            //资产类型
            $type_list = Db::name('cmap_cate')
                ->where(['is_del' => 0])
                ->order('sort desc')
                ->select();
            $proviec_list = Db::name('area')->where([
                ['id', '<>', 0],
                ['pid', '=', 0]
            ])->select();

            $provice = Db::name('area')->where(['name' => $data['province']])->find();
            $city_list = Db::name('area')->where([
                'pid' => $provice['id']
            ])->select();
            $city = Db::name('area')->where(['name' => $data['city']])->find();

            $area_list = Db::name('area')->where([
                'pid' => $city['id']
            ])->select();
            if ($area_list[0]['name'] == "市辖区") {
                $area_list2 = Db::name('area')->where([
                    'pid' => $area_list[0]['id']
                ])->select();
                foreach ($area_list2 as $k => $v) {
                    $area_list[] = $v;
                }
                unset($area_list[0]);
            }
            usort($area_list, function ($a, $b) {
                return $a['id'] - $b['id'];
            });
            if ($data['start_time']) {
                $data['start_time'] = date('Y-m-d H:i:s', $data['start_time']);
            }

            $this->assign([
                'data' => $data,
                'proviec' => $proviec_list,
                'city_list' => $city_list,
                'area_list' => $area_list,
                'type_list' => $type_list,
            ]);
            return $this->fetch();
        }
    }

    public function getc_cate()
    {
        $id = input('id');
        $detail = Db::name('area')->where('name', $id)->find();
        $query = db('area')->where('pid', $detail['id'])->select();

        if ($query[0]['name'] == "市辖区") {
            $area_list2 = Db::name('area')->where([
                'pid' => $query[0]['id']
            ])->select();
            foreach ($area_list2 as $k => $v) {
                $query[] = $v;
            }
            unset($query[0]);
        }
        usort($query, function ($a, $b) {
            return $a['id'] - $b['id'];
        });

        return json(['code' => 200, 'data' => $query, 'msg' => '获取成功']);
    }

    public function getc_cate2()
    {
        $id = input('id');
        $query = db('area')->where('pid', $id)->select();
        return json(['code' => 200, 'data' => $query, 'msg' => '获取成功']);
    }

    public function import()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['suffix'] = explode('.', $data['excel_url']);
            $data['suffix'] = end($data['suffix']);
            $allow_type = array(
                'xlsx', 'xls', 'csv', 'xlsm', 'xlsb', 'xltm'
            );
            $pattern = '/(http|https):\/\/[^\/]+/';
            $newUrl = preg_replace($pattern, '', $data['excel_url']);
            $newUrl = substr($newUrl, 1);
            if (in_array($data['suffix'], $allow_type)) {
                $spreadsheet = IOFactory::load($newUrl);
                $sheetData = $spreadsheet->getActiveSheet()->toArray();
                unset($sheetData[0]);
                $import_data = array();
                foreach ($sheetData as $k => $v) {
                    //京东链接
                    //  公告名
                    // 公告状态
                    // 资产类型
                    // 债权本金/参考价值
                    // 债权利息
                    // 省
                    // 市
                    // 区/县
                    // 详细地址
                    // 小区名称
                    // 小区楼牌号
                    $import_data[$k]['url'] = $v[0];
                    $import_data[$k]['name'] = $v[1];
                    $status = 9;
                    if ($v[2] == '招商中') {
                        $status = 0;
                    } elseif ($v[2] == '招商结束') {
                        $status = 1;
                    }
                    $import_data[$k]['status'] = $status;
                    $cmap_cate = Db::name('cmap_cate')->where('name', '=', $v[3])->find();
                    if ($cmap_cate) {
                        $import_data[$k]['cate_id'] = $cmap_cate['id'];
                    } else {
                        $import_data[$k]['cate_id'] = -1;
                    }
                    $import_data[$k]['reference_value'] = $v[4];
                    $import_data[$k]['debt_interest'] = $v[5];
                    $import_data[$k]['province'] = $v[6];
                    $import_data[$k]['city'] = $v[7];
                    $import_data[$k]['area'] = $v[8];
                    $import_data[$k]['address'] = $v[9];
                    $import_data[$k]['community'] = $v[10];
                    $import_data[$k]['community_detail'] = $v[11];
                    $import_data[$k]['img'] = config('site.web_img_url');
                    $import_data[$k]['zong_value'] = $v[4] + $v[5];
                    $import_data[$k]['create_time'] = time();
                    $import_data[$k]['xm_type'] = 2;
                }
                $result = Db::name('cmap')->insertAll($import_data);
                $this->success("增加完成");
            } else {
                $this->error('请上传正确格式文件');
            }
        }
        return $this->fetch('');
    }

}